 package com.cr.api.controller;
 


import com.cr.api.vo.PayCallBackVo;
import com.cr.business.domain.*;
import com.cr.business.service.*;
import com.cr.common.core.controller.BaseController;
import com.cr.common.core.domain.AjaxResult;
import com.cr.common.utils.http.HttpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.*;

 @Api(description = "支付相关接口-目前只需要获取支付二维码")
 @RestController
 @RequestMapping({"/api/pay"})
@CrossOrigin
 @Slf4j
 public class ApiPayController extends BaseController {
     @Value("${pay.nativePayUrl}")
     private String nativePayUrl;
     @Autowired
     private ICrSourceEntrustmentService sourceEntrustmentService;
     @Autowired
     private ICrPayInfoService payInfoService;
     @Autowired
     private ICrPlatformPriceSettingService crPlatformPriceSettingService;
     @Autowired
     private ICrSourceEntrustmentService crSourceEntrustmentService;
     @Autowired
     private ICrAmountFlowService crAmountFlowService;
     @Autowired
     private ICrAnswerService crAnswerService;
     @GetMapping({"/qrcode"})
     @ResponseBody
     @ApiOperation("获取支付二维码")
     @ApiImplicitParams({
             @ApiImplicitParam(name = "id", value = "债权线索或交易Id 如果是金点子服务 就是律师id", dataType = "数值型"),
             @ApiImplicitParam(name = "type", value = "clue：有偿征集债务人信息 tran：债权转让 sour:案源委托 serv:金点子付费回答", dataType = "数值型")
     })

     public AjaxResult releaseTransaction(@RequestParam String type, @RequestParam String userId, @RequestParam(required = false) String id) {
         CrPlatformPriceSetting setting = new CrPlatformPriceSetting();
         List<CrPlatformPriceSetting> list = null;
         Double payMoney = 0.0;
         String orderNo = type + "_" + userId +"_" + id+"_"+System.currentTimeMillis() ;
         if ("clue".equalsIgnoreCase(type)) {
             setting.setPlaKey("debt");
         } else if("tran".equalsIgnoreCase(type)){
             setting.setPlaKey("tran");
         } else if ("serv".equalsIgnoreCase(type)) {
             setting.setPlaKey("service"); //金点子
         } else {
             setting.setPlaKey("sour");//案源
         }
         list = this.crPlatformPriceSettingService.selectCrPlatformPriceSettingList(setting);
         payMoney = Double.parseDouble(list.get(0).getPlaValue()) * 100;
         if (payMoney > 0) {
             String param = "attach=债权商品&body=债权商品购买&charset=UTF-8&mch_create_ip=123.12.12.123" +
                     "&out_trade_no=" + orderNo + "&service=unified.trade.native&sign_type=RSA_1_256&total_fee=" + payMoney.intValue() + "&version=2.0";
             String response = HttpUtils.sendPost(nativePayUrl, param);
             if(StringUtils.isNotEmpty(response)){
                 Map<String,Object> result = new HashMap();
                 result.put("qrcodeUrl",response);
                 result.put("orderNo",orderNo);
                 result.put("payAmount",payMoney/100);
                 return AjaxResult.success("获取成功",result);
             }

         }
         return AjaxResult.error("获取失败,请重试");
     }

     /**
      * 支付时的回调函数
      */
     @PostMapping(value = "/payCallBack",consumes = "text/xml;charset=ISO-8859-1", produces = "application/xml")
     public void payCallBack(@RequestBody PayCallBackVo vo){
         log.debug("pay call back called---------------------"+vo);

         String orderNo = vo.getOut_trade_no();
         String[] orderInfo = orderNo.split("_");
         Integer type = 0;//债权交易
         if(orderInfo.length>2&&(
                 orderInfo[0].equalsIgnoreCase("clue")||
                         orderInfo[0].equalsIgnoreCase("tran"))){
             if(orderInfo[0].equalsIgnoreCase("clue")){
                 type = 1; //线索
             }
             CrPayInfo payInfo = new CrPayInfo();
             payInfo.setDataId(Long.parseLong(orderInfo[2]));
             payInfo.setDataType(orderInfo[0].equalsIgnoreCase("clue")?1l:2l);
             payInfo.setUserId(Long.parseLong(orderInfo[1]));
             payInfo.setPayTime(new Date());
             payInfo.setStatus(0);
             payInfo.setPayAmount(new BigDecimal(vo.getTotal_fee()));
             payInfo.setPayResult(Integer.parseInt(vo.getPay_result()));//支付结果 0 成功 1 失败
             payInfo.setOrderNo(orderNo);
             payInfoService.insertCrPayInfo(payInfo);
         }
         if(orderInfo[0].equalsIgnoreCase("serv")){//如果是金点子付费回答
             type = 2;
             CrAnswer answer = new CrAnswer();
             answer.setParentAnswerId(Long.parseLong(orderInfo[2]));
             answer.setPayResult(Integer.parseInt(vo.getPay_result()));
             crAnswerService.updateCrAnswer(answer);
         }else if(orderInfo[0].equalsIgnoreCase("sour")){
             type = 3;
             CrSourceEntrustment record = new CrSourceEntrustment();
             record.setPayStatus(1);
             record.setPayAmount(new BigDecimal(vo.getTotal_fee()));
             record.setPayTime(new Date());
             record.setEntrustId(Long.parseLong(orderInfo[2]));
             record.setPayStatus(Integer.parseInt(vo.getPay_result()));
             record.setOrderNo(orderNo);
             crSourceEntrustmentService.updateCrSourceEntrustment(record);
         }
         CrAmountFlow flow = new CrAmountFlow();
         flow.setAmount(new BigDecimal(vo.getTotal_fee()));
         flow.setUserId(Long.parseLong(orderInfo[1]));
         flow.setMode(1);//指出
         flow.setType(type);
         crAmountFlowService.insertCrAmountFlow(flow);
     }

     @GetMapping({"/payresult"})
     @ResponseBody
     @ApiOperation("获取支付结果")
     @ApiImplicitParams({
             @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "数值型")
     })

     public AjaxResult releaseTransaction(@RequestParam @NotNull String orderNo) {
         String[] order = orderNo.split("_");
         if(orderNo.split("_").length>1){
             Map<String,Object> result = new HashMap();
             String type = order[0];
             if ("clue".equalsIgnoreCase(type) || "tran".equalsIgnoreCase(type)) {
                 CrPayInfo info = new CrPayInfo();
                 info.setOrderNo(orderNo);
                 List<CrPayInfo> list = this.payInfoService.selectCrPayInfoList(info);
                 if(CollectionUtils.isNotEmpty(list)){
                     result.put("payResult",list.get(0).getPayResult());
                     return AjaxResult.success(result);
                 }
             } else if ("serv".equalsIgnoreCase(type)) {
//                 CrServiceApplicationRecord info = new CrServiceApplicationRecord();
//                 info.setOrderNo(orderNo);
//                 List<CrServiceApplicationRecord> list = this.crServiceApplicationRecordService.selectCrServiceApplicationRecordList(info);
//                 if(CollectionUtils.isNotEmpty(list)){
//                     result.put("payResult",list.get(0).getPayResult());
//                     return AjaxResult.success(result);
//                 }
             } else {
                 CrSourceEntrustment info = new CrSourceEntrustment();
                 info.setOrderNo(orderNo);
                 List<CrSourceEntrustment> list = this.sourceEntrustmentService.selectCrSourceEntrustmentList(info);
                 if(CollectionUtils.isNotEmpty(list)){
                     result.put("payResult",list.get(0).getPayResult());
                     return AjaxResult.success(result);
                 }
             }
             return AjaxResult.error("订单号不存在");
         }
         return AjaxResult.error("订单号格式不正确");
     }
 }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-api-4.7.0.jar!\com\cr\api\controller\ApiDebtTransactionController.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */